package com.aha.dataanalyzeandview.controller;


import com.aha.dataanalyzeandview.entity.base.ResultEntity;
import com.aha.dataanalyzeandview.entity.metadata.ConnectionInfoEntity;
import com.aha.dataanalyzeandview.service.IConnectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/autoReport")
public class AutoReportController {

    @Autowired
    private IConnectionService connectionService;



    @GetMapping(value = "/queryAllConnectionInfos")
    public ResultEntity queryAllConnectionInfos(HttpServletRequest r) {
        Map<String, Object> paramMap = new HashMap<>();

        return ResultEntity.success(connectionService.queryAllConnectionInfo(paramMap));
    }


    @PostMapping(value = "/testConnection")
    public ResultEntity testConnection(ConnectionInfoEntity connectionInfoEntity){
        boolean res = connectionService.testConnection(connectionInfoEntity);
        if(res) {
            return ResultEntity.success(res);
        }else{
            return ResultEntity.error(-222,"测试连接失败！");
        }
    }


    @PostMapping(value = "/addConnectionInfo")
    public ResultEntity addConnectionInfo (ConnectionInfoEntity connectionInfoEntity){
        boolean connRes = connectionService.testConnection(connectionInfoEntity);
        if(connRes){
            return ResultEntity.error(-222,"测试连接失败！");
        }
        int res = connectionService.addConnectionInfo(connectionInfoEntity);
        if(res>0) {
            return ResultEntity.success(res);
        }else{
            return ResultEntity.error(-222,"添加数据源失败！");
        }
    }








}



